package com.ajhlp.app.phoneDetailQuery.service;

import java.io.File;
import java.util.List;

import javax.annotation.PostConstruct;

import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;

import com.ajhlp.app.phoneDetailQuery.db.ConfigurationDao;
import com.ajhlp.app.phoneDetailQuery.model.Configuration;
import com.ajhlp.app.phoneDetailQuery.model.ConfigurationNameEnum;

@Transactional
public class SystemConfigModifyService implements IConfigModifyService {
	
	private ConfigurationDao dao;
	
	
	public ConfigurationDao getDao() {
		return dao;
	}

	public void setDao(ConfigurationDao dao) {
		this.dao = dao;
	}

	
	private Logger log = Logger.getLogger(getClass());
	
	
	public static final boolean isAllConfigExist(){
		if(configProperties.containsKey(ConfigurationNameEnum.SAVE_PATH.getName()) && 
				configProperties.get(ConfigurationNameEnum.SAVE_PATH.getName()) != null &&
				!configProperties.get(ConfigurationNameEnum.SAVE_PATH.getName()).equals("")){
			return true;
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.ajhlp.app.phoneDetailQuery.service.IConfigModifyService#readConfig()
	 */
	@PostConstruct
	public void readConfig(){
		List<Configuration> configs = dao.searchAllConfig();
		
		for(int i=0;i<configs.size();i++){
			configProperties.put(configs.get(i).getName(), configs.get(i).getValue());
		}
//		String query_url = configProperties.getQuery_url();
//		String save_path = configProperties.getSave_path();
//		int query_num = configProperties.getQuery_num();
//		
//		synchronized (config) {
//			try {
//				if(loaded == false){
//					config.load(configFileName);
//					config.setReloadingStrategy(new FileChangedReloadingStrategy());
//					loaded = true;
//					log.debug("init XMLConfiguration and set properties");
//				}
//				
//				configProperties.setQuery_url(config.getString("query_url"));
//				configProperties.setQuery_num(config.getInt("query_num"));
//				configProperties.setSave_path(config.getString("save_path"));
//				
//			} catch (ConfigurationException e) {
//				// TODO Auto-generated catch block
//				log.error("", e);
//				configProperties.setQuery_url(query_url);
//				configProperties.setQuery_num(query_num);
//				configProperties.setSave_path(save_path);
//			} 
//		}
	}
	
	/* (non-Javadoc)
	 * @see com.ajhlp.app.phoneDetailQuery.service.IConfigModifyService#saveConfig(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	public void saveConfig(String query_url,String save_path,String query_num,String rule_min_v) throws RuntimeException{
		
		if(save_path != null && !save_path.endsWith(File.separator)){
			save_path += File.separator;
		}
		
		Configuration configuration = new Configuration();
		configuration.setName(ConfigurationNameEnum.QUERY_URL.getName());
		configuration.setValue(query_url);
		
		dao.updateConfig(configuration);
		
		configuration.setName(ConfigurationNameEnum.QUERY_TERM_TIME.getName());
		configuration.setValue(query_num);
		
		dao.updateConfig(configuration);
		
		configuration.setName(ConfigurationNameEnum.SAVE_PATH.getName());
		configuration.setValue(save_path);
		
		dao.updateConfig(configuration);
		
		configuration.setName(ConfigurationNameEnum.RULE_MIN_V.getName());
		configuration.setValue(rule_min_v);
		
		dao.updateConfig(configuration);
		
		configProperties.put(ConfigurationNameEnum.QUERY_URL.getName(), query_url);
		configProperties.put(ConfigurationNameEnum.QUERY_TERM_TIME.getName(), query_num);
		configProperties.put(ConfigurationNameEnum.SAVE_PATH.getName(), save_path);
		configProperties.put(ConfigurationNameEnum.RULE_MIN_V.getName(), rule_min_v);
		
		log.info("update system config success");
//		synchronized (config) {
//			FileOutputStream fos = null;
//			try {
//				if(loaded == false){
//					config.load(configFileName);
//					config.setReloadingStrategy(new FileChangedReloadingStrategy());
//					loaded = true;
//					log.debug("init XMLConfiguration and set properties");
//				}
//				
//				config.setProperty("query_url",query_url);
//				config.setProperty("query_num",query_num);
//				config.setProperty("save_path",save_path);
//				
//				fos = new FileOutputStream(this.getClass().getClassLoader().getResource(configFileName).getPath(), false);
//				
//				config.save(fos,"UTF-8");
//				
//				configProperties.setQuery_url(query_url);
//				configProperties.setQuery_num(Integer.parseInt(query_num));
//				configProperties.setSave_path(save_path);
//				
//				log.info("save config into config.xml success");
//			} catch (Exception e) {
//				// TODO Auto-generated catch block
//				log.error("", e);
//				throw new RuntimeException("");
//			} finally {
//				if(fos != null){
//					try {
//						fos.close();
//					} catch (IOException e) {
//						// TODO Auto-generated catch block
//						log.error(e);
//					}
//				}
//			}
//		}
	}
}
